package com.xuzhijie.pms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuzhijie.pms.common.Constants;
import com.xuzhijie.pms.common.Result;
import com.xuzhijie.pms.entity.*;
import com.xuzhijie.pms.entity.dto.PetDto;
import com.xuzhijie.pms.entity.vo.PetVo;
import com.xuzhijie.pms.entity.vo.PropertyVo;
import com.xuzhijie.pms.exception.PmsServerException;
import com.xuzhijie.pms.mapper.OwnerMapper;
import com.xuzhijie.pms.mapper.PetMapper;
import com.xuzhijie.pms.service.IPetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 许志杰
 * @since 2024-02-16
 */
@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements IPetService {

    @Resource
    private OwnerMapper ownerMapper;

    @Override
    public Result findPage(Integer page, Integer size, Integer gender, Integer isCheckIn, Integer isLogOut, String variety,String color) {
        Page<Pet> petPage = new Page<>(page,size);
        QueryWrapper<Pet> queryWrapper = new QueryWrapper<>();
        Page<Pet> result;
        Long total;

        if (!gender.equals(0)){
            queryWrapper.eq("gender",gender);
        }

        if (!isCheckIn.equals(0)){
            queryWrapper.eq("is_check_in",isCheckIn);
        }

        if (!isLogOut.equals(0)){
            queryWrapper.eq("is_log_out",isLogOut);
        }

        if (StrUtil.isNotBlank(variety)){
            queryWrapper.like("variety",variety);
        }

        if (StrUtil.isNotBlank(color)){
            queryWrapper.like("color",color);
        }

        try {
            result = page(petPage,queryWrapper);
            total = count(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            throw new PmsServerException("业务繁忙", Constants.SYSTEM_ERROR);
        }

        List<Pet> propertyList = result.getRecords();
        List<PetVo> voList = new ArrayList<>();
        for (Pet pet : propertyList) {
            PetVo petVo = new PetVo(pet);
            BeanUtil.copyProperties(pet,petVo);
            voList.add(petVo);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("records",voList);
        resultMap.put("total",total);

        return Result.success(resultMap);
    }

    @Override
    public Result unbindingPet(String id) {
        if (StrUtil.isBlank(id)){
            return Result.error();
        }

        Pet one = getById(id);
        one.setMaster("");
        one.setIsCheckIn(2);
        one.setIsLogOut(1);
        updateById(one);

        return Result.success();
    }

    @Override
    public Result createPet(PetDto petDto) {
        if (BeanUtil.isEmpty(petDto)){
            return Result.error();
        }
        QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",petDto.getPhone());
        System.out.println(petDto.getPhone());
        Owner owner = ownerMapper.selectOne(queryWrapper);
        if (BeanUtil.isEmpty(owner)){
            return Result.error();
        }

        Pet pet = new Pet();
        BeanUtil.copyProperties(petDto,pet);
        pet.setIsCheckIn(1);
        pet.setIsLogOut(2);
        pet.setMaster(owner.getId());
        String[] images = petDto.getImages();
        pet.setImages(Arrays.toString(images));

        save(pet);

        return Result.success();
    }

    @Override
    public Result getByOwnerId(String id) {
        QueryWrapper<Pet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master",id);
        List<Pet> list = list(queryWrapper);
        List<PetVo> voList = new ArrayList<>();
        for (Pet pet : list) {
            PetVo propertyVo = new PetVo(pet);
            BeanUtil.copyProperties(pet,propertyVo,"images");
            voList.add(propertyVo);
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("petList",voList);
        resultMap.put("total",voList.size());

        return Result.success(resultMap);
    }
}
